package com.ysl.fairy_could_class.controller;

import com.ysl.fairy_could_class.common.exception.BizException;
import com.ysl.fairy_could_class.domain.Class;
import com.ysl.fairy_could_class.service.ClassService;
import com.ysl.fairy_could_class.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

@RestController
@RequestMapping("/class")
@Api(tags = "课程管理")
public class ClassController {

    @Autowired
    ClassService service;


    @Value("${fairy.proflie}")
    private String UPLOADPATH;


    @GetMapping("/getClassListAll")
    @ApiOperation(value = "获取课程列表")
    public AjaxResult getClassListAll() {
        System.out.println(service.selectAll());
        return AjaxResult.success(service.selectAll());
    };
    @GetMapping("/getClassById")
    @ApiOperation(value = "根据id查询课程信息")
    public AjaxResult getClassById(int id) {
        System.out.println(service.selectById(id));
        return AjaxResult.success(service.selectById(id));
    }
    @PostMapping("/addClass")
    @ApiOperation(value = "新增课程")
    public AjaxResult addClass(@RequestBody Class lesson) {
        return AjaxResult.success(service.SaveClass(lesson));
    }
    @PostMapping("/UpdateClass")
    @ApiOperation(value = "修改课程类型")
    public AjaxResult UpdateClass(@RequestBody Class lesson){
        return AjaxResult.success(service.UpdateClass(lesson));
    }
    @PostMapping("/DeleteClass")
    @ApiOperation(value = "删除课程")
    public AjaxResult DeleteClass(int id) {
        return AjaxResult.success(service.DeleteClass(id));
    }

    @ApiOperation(value = "图片上传")
    @PostMapping("/uploadPic")
    public AjaxResult uploadPic(@RequestPart("file") MultipartFile myfile) {
        //先看下文件是否为空
        if (myfile.getSize() == 0) {
            return AjaxResult.error(400,"上传的文件不能为空");
        }
        //判断文件的格式,假设我们只支持 jpg 和 png,jpeg
        String fileName =  myfile.getOriginalFilename();
        String[] arr = fileName.split("\\.");
        if (arr.length <1) {
            return AjaxResult.error(401,"文件格式不正确");
        }
        String suffix = arr[arr.length-1];
        if (!Objects.equals(suffix, "jpg") && !Objects.equals(suffix, "png")) {
            return AjaxResult.error(401,"文件格式不正确");
        }
        //看一下文件上传的地址是否存在
        if (UPLOADPATH.startsWith("./")) {
            UPLOADPATH = UPLOADPATH.replace("./", System.getProperty("user.dir") + "/");
        }
        File path = new File(UPLOADPATH);
        if (!path.exists()) {
            //如果文件路径不存在，我们在代码里进行创建
            Boolean result = path.mkdirs();
            if (!result) {
                return AjaxResult.error(399,"目录创建失败");
            }
        }
        //创建文件
        File newFile = new File(path + "//"+ fileName);
        //System.out.println("路径是:" + path + "/"+ fileName);
        try {
            myfile.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
            throw new BizException(402,"上传文件失败");
        }
        return AjaxResult.success();

    }
}
